﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
using DTO;

namespace EC21_EDiamond
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        private SanPhamDTO spDTO = new SanPhamDTO();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["id"] == null)
                {
                    Response.Redirect("./Default.aspx");
                }
                else
                {
                    string id = Request.QueryString["id"].ToString();
                    rpThuongHieu.DataSource = ThuongHieuBUS.DSTH();
                    rpThuongHieu.DataBind();
                    SanPhamBUS.UpdateView(Int32.Parse(id));

                    SanPhamDTO sp = new SanPhamDTO();
                    sp = SanPhamBUS.ChiTietSanPham(Int32.Parse(id));
                    lbTenSP.Text = sp.TenSP.ToString();
                    lbTenSP2.Text = lbTenSP.Text;
                    lbMaSP.Text = sp.MaSP.ToString();
                    lbMoTa.Text = sp.Mota.ToString();
                    lbMoTa2.Text = lbMoTa.Text;
                    String giagoc = String.Format("{0:#,###}", sp.Giagoc);
                    String giakm = String.Format("{0:#,###}", sp.GiaKM);
                    if (sp.GiaKM == 0)
                    {
                        lbGiaGoc.Text = "";
                        lbGiaKM.Text = giagoc + " VNĐ";
                    }
                    else
                    {
                        lbGiaGoc.Text = giagoc + " VNĐ";
                        lbGiaKM.Text = giakm + " VNĐ";
                    }
                    lbChatLieu.Text = sp.Chatlieu.ToString();
                    lbTrongLuong.Text = sp.Trongluong.ToString() + " ctw";
                    lbLoaiDa.Text = sp.Loaida.ToString();
                    lbTenSP3.Text = lbTenSP.Text;
                    hplDanhMuc.Text = DanhMucBUS.TenDanhMuc(sp.MaDM);
                    hplDanhMuc.NavigateUrl = "./Product.aspx?id=" + sp.MaDM;
                    zoom1.NavigateUrl = sp.HinhAnh;
                    imgAnh.ImageUrl = sp.HinhAnh;
                    //rpAnhPhu.DataSource = SanPhamBUS.DsHinhAnh(sp.MaSP);
                    //rpAnhPhu.DataBind();
                    rpSanPhamCungLoai.DataSource = SanPhamBUS.SanPhamCungLoai(sp.MaDM);
                    rpSanPhamCungLoai.DataBind();

                    spDTO = SanPhamBUS.ChiTietSanPham(Int32.Parse(id));
                    DataTable dt = new DataTable();
                    DataTable db = new DataTable();
                    int sl = 0;
                    if (Session["GioHang"] != null)
                    {
                        dt = (DataTable)Session["GioHang"];
                        sl = dt.Rows.Count;
                    }
                    hplGioHang.NavigateUrl = "./Cart.aspx";
                    hplGioHang.Text = "Giỏ Hàng(" + sl + ")";

                    DataTable spdx;
                    if (Session["SanPhamDaXem"] == null)
                    {
                        spdx = new DataTable();
                        spdx.Columns.Add("MaSP");
                        spdx.Columns.Add("TenSP");
                        spdx.Columns.Add("GiaKM");
                        spdx.Columns.Add("GiaGoc");
                        spdx.Columns.Add("HinhAnh");
                    }
                    else
                    {
                        spdx = (DataTable)Session["SanPhamDaXem"];
                    }
                    if (CheckProduct(Int32.Parse(id)) == false)
                    {
                        DataRow dr = spdx.NewRow();
                        dr["MaSP"] = Int32.Parse(id);
                        dr["TenSP"] = spDTO.TenSP;
                        dr["GiaKM"] = spDTO.GiaKM;
                        dr["GiaGoc"] = spDTO.Giagoc;
                        dr["HinhAnh"] = spDTO.HinhAnh;
                        spdx.Rows.Add(dr);
                    }
                    Session["SanPhamDaXem"] = spdx;
                    if (Session["SanPhamDaXem"] != null)
                    {
                        rpSanPhamDaXem.DataSource = (DataTable)Session["SanPhamDaXem"];
                        rpSanPhamDaXem.DataBind();
                    }
                }
            }
        }

        bool CheckProduct(int maSP)
        {
            bool flag = false;
            if(Session["GioHang"] != null)
            {
                DataTable dtCheck = (DataTable)Session["GioHang"];
                if (dtCheck.Rows.Count > 0)
                {
                    for (int i = 0; i < dtCheck.Rows.Count; i++)
                    {
                        if (Int32.Parse(dtCheck.Rows[i]["MaSP"].ToString()) == maSP)
                        {
                            flag = true;
                            return flag;
                        }
                    }
                }
                else {
                    flag = false;
                }
            }
            return flag;
        }

        protected void btnOneClick_Click(object sender, EventArgs e)
        {
            spDTO = SanPhamBUS.ChiTietSanPham(Int32.Parse(Request.QueryString["id"].ToString()));
            DataTable dt;
            if (Session["GioHang"] == null)
            {
                dt = new DataTable();
                dt.Columns.Add("MaSP");
                dt.Columns.Add("TenSP");
                dt.Columns.Add("Gia");
                dt.Columns.Add("SoLuong");
                dt.Columns.Add("HinhAnh");
                dt.Columns.Add("TongTien");
            }
            else
            {
                dt = (DataTable)Session["GioHang"];
            }
            string id = Request.QueryString["id"].ToString();
            if (CheckProduct(Int32.Parse(id)) == false)
            {
                DataRow dr = dt.NewRow();
                dr["MaSP"] = Int32.Parse(id);
                dr["TenSP"] = spDTO.TenSP;
                int gia = 0;
                if (spDTO.GiaKM == 0)
                {
                    gia = spDTO.Giagoc;
                }
                else
                {
                    gia = spDTO.GiaKM;
                }
                dr["Gia"] = gia;
                dr["SoLuong"] = Int32.Parse(ddlSoLuong.SelectedValue);
                dr["HinhAnh"] = spDTO.HinhAnh;
                dr["TongTien"] = gia * Int32.Parse(ddlSoLuong.SelectedValue);
                dt.Rows.Add(dr);
            }
            Session["GioHang"] = dt;
            Response.Redirect("CheckOut.aspx");
        }

        protected void btnMuaHang_Click(object sender, EventArgs e)
        {
            spDTO = SanPhamBUS.ChiTietSanPham(Int32.Parse(Request.QueryString["id"].ToString()));
            DataTable dt;
            if (Session["GioHang"] == null)
            {
                dt = new DataTable();
                dt.Columns.Add("MaSP");
                dt.Columns.Add("TenSP");
                dt.Columns.Add("Gia");
                dt.Columns.Add("SoLuong");
                dt.Columns.Add("HinhAnh");
                dt.Columns.Add("TongTien");
            }
            else
            {
                dt = (DataTable)Session["GioHang"];
            }
            string id  = Request.QueryString["id"].ToString();
            if (CheckProduct(Int32.Parse(id)) == false)
            {
                DataRow dr = dt.NewRow();
                dr["MaSP"] = Int32.Parse(id);
                dr["TenSP"] = spDTO.TenSP;
                int gia = 0;
                if(spDTO.GiaKM == 0)
                {
                    gia = spDTO.Giagoc;
                }else {
                    gia = spDTO.GiaKM;
                }
                dr["Gia"] = gia;
                dr["SoLuong"] = Int32.Parse(ddlSoLuong.SelectedValue);
                dr["HinhAnh"] = spDTO.HinhAnh;
                dr["TongTien"] = gia * Int32.Parse(ddlSoLuong.SelectedValue);
                dt.Rows.Add(dr);
            }
            Session["GioHang"] = dt;
            Response.Redirect("Cart.aspx");
        }
    }
}